package br.org.cesar.common;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import br.org.cesar.util.Utils;



/*
 * Executa todas as acoes na pagina Inicial
 */
public class EditingFields {

	/*
	 * Instancia privada do webDriver que vira da suite de teste
	 */
	private static final WebDriver driver;
	private static final WebDriverWait wait;

	/*
	 * Construtor que ira adicionar a instancia do WebDriver para utilizacao dos metodos
	 */
	static {
		driver = Selenium.getDriver();
		wait = new WebDriverWait(driver, 10);
	}
	
	static By clickNameField = By.id("name_rg_display_section");
	static By nameField = By.id("nome_pessoa");
	static By clickSaveButtonName = By.cssSelector("input[type=\"button\"]");
	static By verifySaveName = By.id("name_hv_saving_section");
	static By clickEmailField = By.id("email_rg_display_section");
	static By emailField = By.id("email_value");
	static By clickSaveButtonEmail = By.cssSelector("#email_hv_editing_section > input[type=\"button\"]");
	static By verifySaveEmail = By.id("email_hv_saving_section");
	static By clickPhoneField = By.id("phone_rg_display_section");
	static By phoneField = By.id("phone_value");
	static By clickSaveButtonPhone = By.cssSelector("#phone_hv_editing_section > input[type=\"button\"]");
	static By verifySavePhone = By.id("phone_hv_saving_section");
	static String valueNameField = "Desafio Selenium2";
	static String valueEmailField = "desafioselenium2@gmail.com";
	static String valuePhoneField = "819999-9999";
			
	public EditingFields() {
	}

	/**
	 * Fill Name Field
	 */
	public static void fillNameField(){
		Utils.isVisible(clickNameField);
		driver.findElement(clickNameField).click();
		driver.findElement(nameField).clear();
		driver.findElement(nameField).sendKeys(valueNameField);
	}	
	/**
	 * click the save button name
	 */
	
	public static void saveName(){
		driver.findElement(clickSaveButtonName).click();
	}
	
	/**
	 * Verify save name field
	 */
	
	public static void verifySaveNameField(){
		wait.until(ExpectedConditions.visibilityOfElementLocated(verifySaveName));
		Assert.assertEquals("Salvando...", driver.findElement(verifySaveName).getText());
		wait.until(ExpectedConditions.visibilityOfElementLocated(clickNameField));
//		Utils.takeScreenshot("NameTestScreenshot001");
	}

	/**
	 * Fill Email Field
	 */
	public static void fillEmailField(){
		Utils.isVisible(clickEmailField);
		driver.findElement(clickEmailField).click();
		driver.findElement(emailField).clear();
		driver.findElement(emailField).sendKeys(valueEmailField);

	}	
	/**
	 * click the save button email
	 */
	
	public static void saveEmail(){
		driver.findElement(clickSaveButtonEmail).click();
	}
	
	/**
	 * Verify save email
	 */
	
	public static void verifySaveEmailField(){
		wait.until(ExpectedConditions.visibilityOfElementLocated(verifySaveEmail));
		Assert.assertEquals("Salvando...", driver.findElement(verifySaveEmail).getText());
		wait.until(ExpectedConditions.visibilityOfElementLocated(clickEmailField));
//		Utils.takeScreenshot("EmailTestScreenshot002");
	}

	/**
	 * Fill phone Field
	 */
	public static void fillPhoneField(){
		Utils.isVisible(clickPhoneField);
		driver.findElement(clickPhoneField).click();
		driver.findElement(phoneField).clear();
		driver.findElement(phoneField).sendKeys(valuePhoneField);
		
	}	
	/**
	 * click the save button phone
	 */
	
	public static void savePhoneField(){
		driver.findElement(clickSaveButtonPhone).click();
	}
	
	/**
	 * Verify save Phone
	 */
	
	public static void verifySavePhoneField(){
		wait.until(ExpectedConditions.visibilityOfElementLocated(verifySavePhone));
		Assert.assertEquals("Salvando...", driver.findElement(verifySavePhone).getText());
		wait.until(ExpectedConditions.visibilityOfElementLocated(clickPhoneField));
//		Utils.takeScreenshot("PhoneTestScreenshot003");
	}

	/**
	 * Verifications
	 */
	
	@SuppressWarnings("deprecation")
	public static void verificationsFields(){
		Assert.assertEquals(valueNameField, driver.findElement(clickNameField).getText());
		Assert.assertEquals("Email: " + valueEmailField, driver.findElement(clickEmailField).getText());
		Assert.assertEquals("Telefone: " + valuePhoneField, driver.findElement(clickPhoneField).getText());
		Utils.wait(3000);
	}

	
}